﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;
using System.Data;

namespace JM.JCY.SharePointComponent.Survey.CreateSurvey
{
    public partial class CreateSurveyUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void imgCreateSurvey_Click(object sender, ImageClickEventArgs e)
        {
            Guid listId = new Guid();
            listId = JCYSharePointHelper.CreateDocumentLibrary(this.txtSurveyName.Text.Trim(), this.txtSurveyDescript.Text, SPListTemplateType.Survey);

            DataBindCurrentList(listId);

            this.lbMessage.Text = "请帮该调查建立相应的问题和回答选项！";

        }

        private void DataBindCurrentList(Guid listId)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns.Add("Link");
            dt.Columns.Add("Desc");
            dt.Columns.Add("Created");
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists.GetList(listId, false);
            
            DataRow newRow = dt.NewRow();
            newRow["Title"] = list.Title.ToString();
            newRow["Link"] = list.DefaultViewUrl.ToString();
            newRow["Desc"] = list.Description;
            newRow["Created"] = list.Created;
            dt.Rows.Add(newRow);
          
            this.gvBC.DataSource = dt.DefaultView;
            this.gvBC.DataBind();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(SPContext.Current.Site.ServerRelativeUrl + "pages/ViewAllApplications.aspx");
        }

        protected void gvBC_DataBound(object sender, GridViewRowEventArgs e)
        {
            var drv = (DataRowView)e.Row.DataItem;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal Link = (Literal)e.Row.Cells[0].FindControl("ltRedirect");
                Link.Text = "<img src='/_layouts/images/itsurvey.png'>&nbsp;&nbsp;<a href='" + drv["Link"].ToString() + "'>" + drv["Title"].ToString() + "</a>";

                Literal Desc = (Literal)e.Row.Cells[0].FindControl("ltDesc");
                Desc.Text = drv["Desc"].ToString();

            }
        }
    }
}
